package servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import util.CookieUtil;

import bean.Cart;
import bean.CartItem;

import dao.ComputerDAO;
import dao.DaoFactory;
import entity.Computer;

public class ShoppingServlet extends HttpServlet{
	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String uri=request.getRequestURI();
		String action=uri.substring(uri.lastIndexOf("/"), uri.lastIndexOf("."));
		if("/list".equals(action)){
			ComputerDAO dao = DaoFactory.getInstance(ComputerDAO.class);
			try {
				List<Computer> computers = dao.findAll();
				request.setAttribute("computers", computers);
				request.getRequestDispatcher("computer_list.jsp").forward(request, response);
			} catch (Exception e) {
				e.printStackTrace();
				throw new ServletException(e);
			}
		}else if("/buy".equals(action)){
			long id = Long.parseLong(request.getParameter("id"));
			ComputerDAO dao = DaoFactory.getInstance(ComputerDAO.class);
			
			try {
				Computer c = dao.findById(id);
				CartItem item=new CartItem();
				item.setComputer(c);
				
				HttpSession session=request.getSession();
				Cart cart = (Cart) session.getAttribute("cart");
				if(cart==null){
					cart=new Cart();
					String cartInfo=CookieUtil.getCookie("cartInfo",request);
					cart.load(cartInfo);
					session.setAttribute("cart", cart);
				}
				
				boolean itemAdded=cart.add(item);
				if(itemAdded){
					item.setQty(1);
					CookieUtil.saveCookie("cartInfo", cart.store(), response);
					response.sendRedirect("list.do");
				}else{
					request.setAttribute("itemExisted"+id, "商品已购买");
					request.getRequestDispatcher("list.do").forward(request, response);
				}
			} catch (Exception e) {
				e.printStackTrace();
				throw new ServletException(e);
			}
		}else if("/modify".equals(action)){
			long id=Long.parseLong(request.getParameter("id"));
			int qty=Integer.parseInt(request.getParameter("qty"));
			HttpSession session=request.getSession();
			Cart cart = (Cart) session.getAttribute("cart");
			cart.modify(id, qty);
			CookieUtil.saveCookie("cartInfo", cart.store(), response);
			response.sendRedirect("cart.jsp");
		}else if("/delete".equals(action)){
			long id = Long.parseLong(request.getParameter("id"));
			HttpSession session=request.getSession();
			Cart cart = (Cart) session.getAttribute("cart");
			cart.delete(id);
			CookieUtil.saveCookie("cartInfo", cart.store(), response);
			response.sendRedirect("cart.jsp");
		}else if("/clear".equals(action)){
			HttpSession session=request.getSession();
			Cart cart = (Cart) session.getAttribute("cart");
			cart.clear();
			CookieUtil.delete("cartInfo", response);
			response.sendRedirect("cart.jsp");
		}else if("/test".equals(action)){
			request.setAttribute("v","汉字");
			request.getRequestDispatcher("test.jsp").forward(request,response);
		}
	}
}
